package com.ywgt.forkjoin;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ForkJoinTask;
import java.util.stream.LongStream;

public class TestMultply {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        System.out.println(Runtime.getRuntime().availableProcessors());
        test2();
    }

    public static void test1(){
        long start = System.currentTimeMillis();
        Long sum=0L;
        for (Long i=1L;i<=10_0000_0000;i++) {
            sum+=i;
        }
        long end = System.currentTimeMillis();
        System.out.println("sum"+sum+"时间"+(end-start));
    }

    public static void test2() throws ExecutionException, InterruptedException {
        long start = System.currentTimeMillis();
        ForkJoinPool forkJoinPool=new ForkJoinPool();
        ForkJoinTask<Long> fork=new ForkJoinTest(1L,1_0000_0000L);
        ForkJoinTask<Long> task = forkJoinPool.submit(fork);
        Long sum = task.get();
        long end = System.currentTimeMillis();
        System.out.println("sum"+sum+"时间"+(end-start));
    }

    public static void test3(){
        long start = System.currentTimeMillis();
        long sum = LongStream.rangeClosed(0L, 10_0000_0000).parallel().reduce(0, Long::sum);
        long end = System.currentTimeMillis();
        System.out.println("sum"+sum+"时间"+(end-start));
    }
}
